package com.dk.controller;

import com.dk.pojo.SslMenu;
import com.dk.pojo.SslPerson;
import com.dk.service.SslMenuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menuController")
public class MenuController {
    @DubboReference
    private SslMenuService menuService;

    //查询treeView菜单树
    @RequestMapping("/getMenu")
    @ResponseBody
    public List<Map<String,Object>> getMenu(HttpServletRequest request){
        //根据用户id查询菜单树
        SslPerson person = (SslPerson) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> menus = menuService.getMenu(person.getPid());
        return menus;
    }

    //跳转到菜单管理页面
    @RequestMapping("/treeMenu")
    public String treeMenu(){
        return "menu/tree";
    }
    //查询zTree菜单
    @RequestMapping("/getTree")
    @ResponseBody
    public List<Map<String,Object>> getTree(){
        List<Map<String,Object>> menus = menuService.getTree();
        return menus;
    }

    //跳转到添加页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "menu/add";
    }
    //保存菜单
    @RequestMapping("/saveMenu")
    @ResponseBody
    public boolean saveMenu(SslMenu menu){
        menuService.saveMenu(menu);
        return true;
    }

    //修改回填
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        SslMenu menu = menuService.findMenuById(id);
        model.addAttribute("menu", menu);
        return "menu/update";
    }

    //修改菜单
    @RequestMapping("/updateMenu")
    @ResponseBody
    public boolean updateMenu(SslMenu menu){
        menuService.updateMenu(menu);
        return true;
    }
    //删除菜单
    @RequestMapping("/deleteMenu/{id}")
    @ResponseBody
    public boolean deleteMenu(@PathVariable("id") Integer  id){
        menuService.deleteMenu(id);
        return true;
    }

    //查询被选中zTree菜单
    @RequestMapping("/getCheckedTree/{roleId}")
    @ResponseBody
    public List<Map<String,Object>> getCheckedTree(@PathVariable("roleId") Integer roleId){
        List<Map<String,Object>> menus = menuService.getCheckedTree(roleId);
        return menus;
    }
}
